package yxy.game.pm2.command.fortdefeat;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FortDefeatMessage;
import yxy.game.pm2.module.fortdefeat.FortDefeatModule;
import yxy.game.pm2.module.fortdefeat.FortDefeatType;

/** 副本过关斩将 过关斩将 */
@MessageType(proto = Command.FortDefeat, classtype = FortDefeatMessage.FortDefeatRequest.class)
public class CMD_FortDefeat extends Command<FortDefeatMessage.FortDefeatRequest> {

	@Override
	public void execute(MessageSource messageSource, FortDefeatMessage.FortDefeatRequest request) {
		String playerCode = messageSource.getTag();

		FortDefeatMessage.FortDefeatResponse.Builder response = FortDefeatMessage.FortDefeatResponse.newBuilder();

		FortDefeatModule defeatModule = FortDefeatModule.get(playerCode);

		FortDefeatType type = defeatModule.getType();
		if (type == null) {
			response.setType(0);
			response.addSelect(FortDefeatType.普通.getNumber());
			for (FortDefeatType finish : defeatModule.getFinished()) {
				switch (finish) {
				case 困难:
					response.addSelect(FortDefeatType.炼狱.getNumber());
					break;
				case 普通:
					response.addSelect(FortDefeatType.困难.getNumber());
					break;
				default:
					break;
				}
			}
		} else {
			response.setType(type.getNumber());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, FortDefeatMessage.FortDefeatResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}